package com.example.multitenant.controller

import com.example.multitenant.base.Log
import com.example.multitenant.model.Persons
import com.example.multitenant.multitenant.config.TenantContext
import com.example.multitenant.multitenant.controller.TenantService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.scheduling.annotation.Async
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Service

/**
 * @author xiang
 * @Date 2022/9/12 8:31
 **/

@Service
class TestService {

    companion object {
        private const val TAG = "TestService"
    }

    @Autowired
    private lateinit var persons: Persons

    @Autowired
    private lateinit var tenantService: TenantService

    @Async("taskExecutor")
    fun testAsync() {
        Log.i(TAG, "start async")
        Log.i(TAG, "tenant: ${TenantContext.getTenantId()}")
        persons.findAll()
        Log.i(TAG, "end async")
    }

    /**
     * 每 30s 执行一次
     */
    @Scheduled(cron = "30 * * * * ?")
    fun testSchedule() {
        Log.i(TAG, "start scheduling")
        tenantService.runOnAllTenants {
            persons.findAll()
        }
        Log.i(TAG, "end scheduling")
    }

}